<template>
  <div class="h100">
    <el-button @click="createPrompt" class="create-prompt"
      >新建prompt</el-button
    >
    <el-tabs v-model="activeName" class="demo-tabs" @tab-change="tabChange">
      <el-tab-pane class="df fdc jcsb" label="速读" name="first">
        <el-table :data="tableData">
          <el-table-column prop="id" label="PromptID"></el-table-column>
          <el-table-column prop="prompt" label="Prompt内容"></el-table-column>
          <el-table-column
            prop="updateDateStr"
            label="创建时间"
          ></el-table-column>
          <el-table-column
            prop="updateDateStr"
            label="更新时间"
          ></el-table-column>
          <el-table-column label="状态">
            <template #default="scope">
              <p
                :style="{
                  color: scope.row.status == 1 ? '#409EFF' : '#606266',
                }"
              >
                {{ scope.row.statusText }}
              </p>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注"></el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <div v-show="scope.row.status != 1">
                <el-button @click="pushOnline(scope.row)" link
                  >推到线上</el-button
                >
                <el-button @click="editSpeedRead(scope.row)" link
                  >编辑</el-button
                >
                <el-button @click="deletePe(scope.row)" link>删除</el-button>
              </div>
              <div v-show="scope.row.status == 1">-</div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 40]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          style="height: 40px; margin-top: 20px"
        />
      </el-tab-pane>
      <el-tab-pane class="df fdc jcsb" label="笔记与评论" name="second">
        <el-table :data="tableData">
          <el-table-column prop="id" label="PromptID"></el-table-column>
          <el-table-column prop="prompt" label="Prompt内容"></el-table-column>
          <el-table-column
            prop="updateDateStr"
            label="创建时间"
          ></el-table-column>
          <el-table-column
            prop="updateDateStr"
            label="更新时间"
          ></el-table-column>
          <el-table-column label="状态">
            <template #default="scope">
              <p
                :style="{
                  color: scope.row.status == 1 ? '#409EFF' : '#606266',
                }"
              >
                {{ scope.row.statusText }}
              </p>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注"></el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <div v-show="scope.row.status != 1">
                <el-button @click="pushOnline(scope.row)" link
                  >推到线上</el-button
                >
                <el-button @click="editNote(scope.row)" link>编辑</el-button>
                <el-button @click="deletePe(scope.row)" link>删除</el-button>
              </div>
              <div v-show="scope.row.status == 1">-</div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="currentPage1"
          v-model:page-size="pageSize1"
          :page-sizes="[10, 20, 30, 40]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          style="height: 40px; margin-top: 20px"
        />
      </el-tab-pane>
    </el-tabs>

    <el-dialog
      v-model="dialogTableVisible"
      title="Shipping address"
      width="800"
    >
      <el-form :model="form">
        <el-form-item label="Prompt内容"></el-form-item>
        <el-form-item label="状态"></el-form-item>
        <el-form-item label="备注"></el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, watch } from "vue";
import { peList, peStatus, delPe } from "@/apis/pe";
import { useCategoryStore } from "@/store";
import { transDate } from "@/utils/transforDate";
import { ElMessageBox, ElMessage } from "element-plus";

const tableData = ref([]);
const form = ref({
  content: "",
  status: "",
  remark: "",
});
const activeName = ref("first");
const dialogTableVisible = ref(false);
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const currentPage1 = ref(1);
const pageSize1 = ref(20);

const props = defineProps({
  data: String,
});
const emit = defineEmits(["backMessage"]);
const store = useCategoryStore();

const getData = async () => {
  try {
    let data = {};
    if (activeName.value == "first") {
      data.page = currentPage.value;
      data.page_size = pageSize.value;
      data.sub_category = "speed_read";
    } else {
      data.page = currentPage1.value;
      data.page_size = pageSize1.value;
      data.sub_category = "note_comment";
    }
    data.category = "info";
    let res = await peList(data);
    if (res.data.code == 1002) {
      tableData.value = res.data.data.items;
      total.value = res.data.data.total;
      tableData.value = tableData.value.map((item) => {
        item.createDateStr = transDate(item.created_at);
        item.updateDateStr = transDate(item.updated_at);
        item.statusText = item.status == 0 ? "未生效" : "线上生效中";
        return item;
      });
    }
  } catch (e) {
    console.log(e);
  }
};

const deletePe = (e) => {
  ElMessageBox.confirm("确定删除此条？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  }).then(async () => {
    try {
      let data = {};
      data.id = e.id;
      let res = await delPe(data);
      if (res.data.code == 200) {
        ElMessage.success("删除成功");
        getData();
      }
    } catch (e) {}
  });
};

const tabChange = () => {
  getData();
};

const handleCurrentChange = (e) => {
  getData();
};

const handleSizeChange = (e) => {
  getData();
};

const createPrompt = () => {
  store.setCategroyMethod("create");
  store.setCurrentCateGroy("");
  store.setCurrentSubCategroy("");
  emit("backMessage", { active: 2 });
  store.setCurrentCateGroy("info");
  if (activeName.value == "first") {
    store.setCurrentSubCategroy("speed_read");
  } else {
    store.setCurrentSubCategroy("note_comment");
  }
};

const editSpeedRead = (e) => {
  store.setCategroyMethod("edit");
  store.setCurrentCateGroy("info");
  store.setCurrentSubCategroy("speed_read");
  emit("backMessage", { active: 2 });
  store.setEditData(e);
};

const editNote = (e) => {
  store.setCategroyMethod("edit");
  store.setCurrentCateGroy("info");
  store.setCurrentSubCategroy("note_comment");
  emit("backMessage", { active: 2 });
  store.setEditData(e);
};

const pushOnline = (e) => {
  ElMessageBox.confirm("是否确定推到线上？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(async () => {
      let data = {};
      data.id = JSON.stringify(e.id);
      data.status = 1;
      try {
        let res = await peStatus(data);
        if (res.data.code == 200) {
          ElMessage.success("成功");
          getData();
        }
      } catch (e) {
        console.log(e, "eee");
      }
    })
    .catch(() => {
      ElMessage.info("取消");
    });
};
watch(
  () => props.data,
  (newVal) => {
    if (newVal == "thrid") {
      getData();
    }
  }
);

watch(
  () => store.currentType,
  (newVal) => {
    if (newVal == "info") {
      getData();
      store.setCurrentType("");
    }
  }
);
</script>

<style lang="scss" scoped>
.create-prompt {
  position: absolute;
  right: 20px;
  z-index: 999;
}
:deep(.el-tabs) {
  height: 100%;
}
:deep(.el-tabs__content) {
  height: 100%;
}
:deep(.el-tab-pane) {
  height: 100%;
}
</style>
